<?php
namespace haibao\app\data;

class AppList extends \haibao\app\data\BaseMysql{

	public function __construct(){
		parent::__construct('\haibao\app\model\data\AppList');
	}
	

	/**
	 * 新增App应用版本
	 */
	public function addVersion($model){
		$this->add($model);
		
		$data = new \haibao\app\cache\applist();
		$data->deleteMaxVersionUrl($model->AppType);
	}
	
	/**
	 * 根据应用版本名称获得一条数据
	 */
	public function getAppVersion($version,$system){
		$versionList = array();
		$result = $this->query("select * from `AppList` where AppVersion = %s and Status = %s and AppType = %s",array($version,\haibao\app\model\data\AppList::STATUS_ENABLE,$system));
		$list = array();
		while($result->fetch_assoc()){
			$list['Id'] = $result->getData('Id');
			$list['AppVersion'] = $result->getData('AppVersion');
			$versionList[] = $list;
		}
		return $versionList;
	}
	
	/**
	 * 根据APP系统获取最高版本下载地址
	 */
	public function getAppUrlBySystem($system){
	    $data = new \haibao\app\cache\applist();
	    $maxVersionUrl = $data->getMaxVersionUrl($system);
	    if(!$maxVersionUrl){
	        $result = $this->query('select Url from AppList where AppVersion=(select max(AppVersion) as AppVersion from AppList where AppType=%s and `Status`=%s) and AppType=%s and `Status`=%s', array(
	            $system, \haibao\app\model\data\AppList::STATUS_ENABLE, $system, \haibao\app\model\data\AppList::STATUS_ENABLE
	        ));
            $retArr = $result->fetch_assoc();
            if($retArr){
	           $maxVersionUrl = $retArr['Url'];
            }
            $data->setMaxVersionUrl($system, $maxVersionUrl);
	    }
	    return $maxVersionUrl;
	}
	
	/**
	 * 删除App应用版本数据
	 */
	public function updateVerStatus($id,$status = \haibao\app\model\data\AppList::STATUS_DISABLE){
		$this->updateById($id, array(\haibao\app\model\filter\AppList::CONDITION_FIELD_NAME_STATUS=>$status));
		
		// 根据记录ID查询系统类型删除缓存中的最高版本记录
	    $app = $this->getOneById($id);
	    $data = new \haibao\app\cache\applist();
	    $data->deleteMaxVersionUrl($app->AppType);
	}
	
}